package com.chanjiedata.changbs.module.main;

/**
 * 文件名 : AllocaPartnerActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/26 15:53
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.AllocPartnerAdapter;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by AndroidIntexh1 on 2018/12/27.
 */

public class AllocaPartnerActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private AllocPartnerAdapter mAdapter;
    private List<Mystock.get_transfer_mb_list.List> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alloca_partner);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initRecyclerView();
        initData();
        backTv.setOnClickListener(view -> finish());
        String title = getIntent().getStringExtra("title");
        tvTitle.setText(title);
    }

    private void initData() {

        String type = getIntent().getStringExtra("type");
        Mystock.get_transfer_mb_list.Builder builder = Mystock.get_transfer_mb_list.newBuilder();
        builder.setRateAction(type);
        NetworkManager.INSTANCE.post(Apis.get_transfer_mb_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                datas = Mystock.get_transfer_mb_list.parseFrom(bytes).getListList();


                mAdapter.addData(datas);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new AllocPartnerAdapter(mContext, datas));
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Mystock.get_transfer_mb_list.List allocaPartnerBean = datas.get(position);
            Intent intent = new Intent();
            intent.putExtra("name", allocaPartnerBean.getMemberName());
            intent.putExtra("phone", allocaPartnerBean.getMemberMobile());
            intent.putExtra("id", allocaPartnerBean.getMemberId());
            setResult(200, intent);
            finish();
        });
    }
}
